# -*- coding: utf-8 -*-
# @Date   : 2020/3/3
# @File   : R52_IG_CASE_PersonalEnforcementRecordCheck_OTO.py
'''eof
name:申请人强制执行记录校验
code:R52_IG_CASE_PersonalEnforcementRecordCheck_OTO
tableName:
columnName:
groups:个人及企业司法校验
dependencies:EG_CASE_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''



import json
from datetime import datetime
import pandas as pd


def FormatData(tableName):
    try:
        eg = EG_CASE_DSJ.get("data").get(tableName)
        return eg if isinstance(eg, list) else [{}]
    except:
        return [{}]


# 统一转成datetime对象，只能转年月日的字符串
def date_format(time):
    import re
    r = re.compile(r'\D+')
    try:
        date = datetime.strptime(time, '%Y.%m.%d').strftime('%Y%m%d')
    except:
        try:
            date = datetime.strptime(time, '%Y-%m-%d').strftime('%Y%m%d')
        except:
            try:
                date = datetime.strptime(time, '%Y/%m/%d').strftime('%Y%m%d')
            except:
                try:
                    tmp = r.sub('', time)
                    date = datetime.strptime(tmp, '%Y%m%d').strftime('%Y%m%d')
                except:
                    date = None
    return date


def float_num(str):
    try:
        return float(str)
    except:
        return None

def getOrdTime(year):
    return (datetime.now() - pd.tseries.offsets.DateOffset(years=year)).strftime("%Y%m%d")

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getRule52():
    try:
        zhixingList = FormatData("R229")
        if zhixingList == [{}]:
            return u'缺失值'
        else:
            if len(zhixingList) >= 1:
                duplicate_list = []
                resultList = []
                for i in zhixingList:
                    EXECMONEY = float_num(i.get("EXECMONEY"))
                    CASECODE = i.get("CASECODE")
                    CASECREATETIME = i.get("CASECREATETIME")
                    if date_format(CASECREATETIME) is not None and float(date_format(CASECREATETIME)) < float(getOrdTime(5)):
                        resultList.append(1)
                        continue
                    if EXECMONEY not in null_type_list and CASECODE not in null_type_list and \
                            CASECREATETIME not in null_type_list and \
                            (CASECREATETIME + CASECODE) not in duplicate_list:
                        duplicate_list.append(CASECREATETIME + CASECODE)
                        if float(date_format(CASECREATETIME)) < float(getOrdTime(5)):
                            resultList.append(1)
                        elif date_format(CASECREATETIME) in null_type_list or EXECMONEY <= 0:
                            resultList.append(2)
                        elif float(date_format(CASECREATETIME)) > float(getOrdTime(0)):
                            resultList.append(2)
                        elif float(getOrdTime(2)) <= float(date_format(CASECREATETIME)) < float(getOrdTime(0)):
                            if EXECMONEY < 6000:
                                resultList.append(1)
                            else:
                                resultList.append(0)
                        elif float(getOrdTime(3)) <= float(date_format(CASECREATETIME)) < float(getOrdTime(2)):
                            if EXECMONEY <= 15000:
                                resultList.append(1)
                            else:
                                resultList.append(0)
                        elif float(getOrdTime(5)) <= float(date_format(CASECREATETIME)) < float(getOrdTime(3)):
                            if EXECMONEY <= 25000:
                                resultList.append(1)
                            else:
                                resultList.append(0)
                    elif EXECMONEY in null_type_list or CASECODE in null_type_list or CASECREATETIME in null_type_list:
                        resultList.append(2)
                if len(resultList) > 0:
                    if min(resultList) == 0:
                        return 0
                    elif max(resultList) == 2:
                        return 2
                    else:
                        return 1
                else:
                    return 1
            else:
                return 1
    except:
        return u'缺失值'


# if __name__ == "__main__":
#     import json
#     with open('C:/Users/zdr/Desktop/samples/EG_CASE_DSJ.json') as ff:
#         EG_CASE_DSJ = json.loads(ff.read())['EG_CASE_DSJ']
#     res = getRule52()
#     print(res)

result = getRule52()

